package com.polaris.live.dialog

import android.content.Context
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogMinimizeBinding
import com.polaris.live.utils.XPopupUtils

/**
 * MinimizeDialog 确认弹窗
 *
 * @author Created by 天晴 on 2024/11/25 09:28
 */
class MinimizeDialog(
    context: Context,
    private val onConfirmClickListener: OnClickListener,
    private val onCancelClickListener: OnClickListener?,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogMinimizeBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_minimize
    }

    private var isBack = false

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogMinimizeBinding.bind(popupImplView)

        mBinding.tvConfirm.setOnSingleClickListener {
            isBack = true
            onConfirmClickListener.onClick(it)
            dismiss()
        }
        mBinding.tvCancel.setOnSingleClickListener {
            isBack = true
            onCancelClickListener?.onClick(it)
            dismiss()
        }

    }

    override fun onDismiss() {
        if (!isBack) {
            onCancelClickListener?.onClick(this)
        }
        super.onDismiss()
    }


    companion object {

        fun show(
            context: Context,
            onConfirmClickListener: OnClickListener,
            onCancelClickListener: OnClickListener?,
        ) {
            XPopupUtils.showViewPopup(
                MinimizeDialog(context, onConfirmClickListener, onCancelClickListener),
                {
                    hasShadowBg(true)
                    isViewMode(false)
                    setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)

                        }
                    })
                },
            )
        }
    }
}